<template>
  <getdatascrollview ref="getdatascrollview" :api="getdata">
    <template #default="{ list }">
      <view
        class="Stationaddbtn u-flex"
        @click="$z.gopage('/pages/ChargeCar/ChargeCarAdd')"
      >
        <view class="iconfont iconjia1 mui-icon"></view>
        {{ $z.$t("添加设备") }}
      </view>
      <view class="" v-for="(item, key) in list" :key="key">
        <stationitem :item="item" v-if="!item.gtel"></stationitem>
        <chargecaritem :item="item" v-else></chargecaritem>
      </view>
    </template>
  </getdatascrollview>
</template>
<script>
  import stationitem from "./StationItem.vue";
  import chargecaritem from "./ChargeCaritem.vue";
  export default {
    components: {
      stationitem,
      chargecaritem,
    },
    props: {
      item: {
        type: Object,
        default: () => {
          return {};
        },
      },
    },
    data() {
      return {
        // list: [],
        StationPid: [],
        ChargeCarPcs: [],
      };
    },
    created() {
      this.init();
    },
    computed: {
      list() {
        let r = [];
        if (this.item.showpcs) {
          r = r.concat(this.StationPid);
        }
        if (this.item.showsb) {
          r = r.concat(this.ChargeCarPcs);
        }
        return r;
      },
    },
    watch: {
      list() {
        this.$refs.getdatascrollview.onRefresh();
      },
    },
    methods: {
      async init() {
        uni.showLoading();
        let SituationTJ = await this.$z.request("/SituationTJ", {
          sum: 1,
          pcs1: this.item.pcs,
        });
        this.SituationTJ = SituationTJ[0];
        let StationPid = await this.$z.request("/StationPid", {
          pid: this.item.pcs,
        });
        StationPid.sort((a, b) => {
          if (a.sjc && b.sjc) {
            if (a.sjc == b.sjc) {
              return a.id - b.id;
            } else {
              var sj = this.$z.timefn.getUTCtime(a.sjc, b.sjc, 1);
              if (sj == 0) {
                return a.id - b.id;
              } else {
                return sj;
              }
            }
          } else {
            return a.id - b.id;
          }
        });
        let ChargeCarPcs = await this.$z.request("/ChargeCarPcs", {
          pcs: this.item.pcs,
        });
        // let list = StationPid.concat(ChargeCarPcs);
        this.StationPid = StationPid;
        this.ChargeCarPcs = ChargeCarPcs;
        // this.list = list;

        this.$refs.getdatascrollview.onRefresh();
      },
      async getdata(reqdata) {
        let list = this.list.slice(reqdata.csta, reqdata.csta + reqdata.cmax);
        let pcslist = list.filter((e) => {
          return !e.gtel && !e.ChargeCarPcs;
        });
        let SituationTJreq = {
          sum: pcslist.length,
        };
        pcslist.forEach((e, i) => {
          SituationTJreq[`pcs${i + 1}`] = e.id;
        });
        if (pcslist.length != 0) {
          let SituationTJ = await this.$z.request(
            "/SituationTJ",
            SituationTJreq
          );
          let SituationTJoption = {};
          SituationTJ.forEach((e) => {
            SituationTJoption[e.pcs] = e;
          });
          list.forEach((e) => {
            if (SituationTJoption[e.id]) {
              e.ChargeCarPcs = SituationTJoption[e.id];
            }
          });
        }
        return list;
      },
    },
  };
</script>
<style lang="scss" scoped>
  .Stationaddbtn {
    margin: 20rpx;
    background: #fff;
    height: 88rpx;
    border-radius: 8rpx;
    display: flex;
    align-items: center;
    justify-content: center;

    .iconfont {
      margin-right: 8rpx;
    }
  }
</style>
